import Vue from "vue";
import Vuex from "vuex";
import persistedState from "@/uni_modules/vuex-persistedstate";
Vue.use(Vuex); // vue的插件机制

// Vuex.Store 构造器选项
const store = new Vuex.Store({
  plugins: [
    persistedState({
      storage: {
        getItem: (key) => uni.getStorageSync(key),
        setItem: (key, value) => uni.setStorageSync(key, value),
        removeItem: (key) => uni.removeStorageSync(key),
      },
    }),
  ],
  // 为了不和页面或组件的data中的造成混淆，state中的变量前面建议加上$符号
  state: {
    // 用户信息
    $userInfo: {},
    $token: "",
  },
  mutations: {
    setUserInfo(state, val) {
      state.$userInfo = val;
    },
    setToken(state, val) {
      state.$token = val;
    },
  },
});

export default store;
